package com.greendragon.persistence.domain; 

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/** 
 * @author wulibin, exceptsky@gmail.com, 2011-9-8 下午11:06:28
 * 
 */
@Entity
@Table(name="articalCategory")
public class ArticalCategory implements Serializable{
	private static final long serialVersionUID = 6300697229353874082L;

	private Long id;
	private String name;													//标题
	private String description;												//描述
	private Boolean block = new Boolean(false);													//是否隐藏
	private Resource coverImage;											//主题封面
	private User author;													//创建者
	private Date createdAt;													//创建日期
	private Date modifiedAt;												//最后修改日期
	private List<Artical> articals;											//包含文章
	private ArticalCategory parentCategory;									//上级分类
	private List<ArticalCategory> childCategories;							//子目录
	
	public ArticalCategory(){}

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name="name", length=50)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Lob
	@Column(name="description")
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Column(name="block")
	public Boolean getBlock() {
		return block;
	}

	public void setBlock(Boolean block) {
		this.block = block;
	}

	@ManyToOne
    @JoinColumn(name="image_id")
	public Resource getCoverImage() {
		return coverImage;
	}

	public void setCoverImage(Resource coverImage) {
		this.coverImage = coverImage;
	}
	
	@ManyToOne
	@JoinColumn(name="author_id")
	public User getAuthor() {
		return author;
	}

	public void setAuthor(User author) {
		this.author = author;
	}

	@Column(name="createdAt")
	@Temporal(TemporalType.TIMESTAMP)
	public Date getCreatedAt() {
		return createdAt;
	}

	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}

	@Column(name="modifiedAt")
	@Temporal(TemporalType.TIMESTAMP)
	public Date getModifiedAt() {
		return modifiedAt;
	}

	public void setModifiedAt(Date modifiedAt) {
		this.modifiedAt = modifiedAt;
	}

	@OneToMany(mappedBy="category",cascade=CascadeType.ALL, fetch=FetchType.LAZY)
	public Collection<Artical> getArticals() {
		return articals;
	}

	public void setArticals(List<Artical> articals) {
		this.articals = articals;
	}

	@ManyToOne
    @JoinColumn(name="parentCategory_id", nullable=true, insertable=false, updatable=false)
	public ArticalCategory getParentCategory() {
		return parentCategory;
	}

	public void setParentCategory(ArticalCategory parentCategory) {
		this.parentCategory = parentCategory;
	}
	
	@OneToMany(mappedBy="parentCategory", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
	public List<ArticalCategory> getChildCategories() {
		return childCategories;
	}

	public void setChildCategories(List<ArticalCategory> childCategories) {
		this.childCategories = childCategories;
	}
	
}
 